#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>

int len = 16;

char randchar(void)
{
    int pos;
    //48~57, 97~122
    do
    {
        pos = (double)rand()/RAND_MAX*127.0;
    }while(pos < '0' || pos > '9' && pos < 'a' || pos > 'z');
    return (char)pos;
}

void randseed(void)
{
#if (defined _WIN32)
    //system("ping -n 2 127.0.0.1 > deleteme.txt");
    //system("del deleteme.txt");
#elif (defined _UNIX)
    //system("ping -c 2 127.0.0.1 > /dev/null");
#else
    #error platform?
#endif
    long i;

    for(i=0;i<0x07ff0000;i++);

    srand((unsigned)clock());
}

int main(int argc,char **argv)
{
    FILE *p;
    int i;

    if(argc > 1 )
    {
        if(strcmp(argv[1],"-l"))
            sscanf(argv[2],"%d",&len);
    }

    printf("Password Generator\n\n");

    srand((unsigned)time(NULL));

    p = fopen("passwd.h","w");
    if(p == NULL)
    {
        printf("Cannot create passwd.h, exiting\n");
        getchar();
        getchar();
        exit(EIO);
    }

    fputs("char unlock_key[]={\"",p);
    for(i=0;i<len;i++)
    {
        fputc(randchar(),p);
        if(i%8 == 0)
            randseed();
    }
    fputs("\"};\r\n",p);

    fputs("char ignition_key[]={\"",p);
    for(i=0;i<len;i++)
    {
        fputc(randchar(),p);
        if(i%8 == 0)
            randseed();
    }
    fputs("\"};\r\n",p);

    fclose(p);

    printf("Saved to passwd.h\n");

    return 0;
}
